﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BanVeXemPhim_Presentation
{
    public class VeDAO : DataProvider
    {
        public void BanVe(VeDTO[] dsve)
        {
            connect();
            int n = dsve.Length;
            for (int i = 0; i < n; i++)
            {
                string query = "insert into Ve(Ghe,DaThanhToan,XuatChieu,IdDoiTuongMua) values(" + dsve[i].Ghe.ToString() + ",1," + dsve[i].XuatChieu.ToString() + "," + dsve[i].IdDoiTuongMua+ ")";
                command = connection.CreateCommand();
                command.CommandText = query;
                command.ExecuteNonQuery();
            }
        }
        public void DatVeTruoc(VeDTO[] dsVe)
        {
            connect();
            command = connection.CreateCommand();
            int n = dsVe.Length;
            for(int i=0;i<n;i++)
            {
                string query = "insert into Ve(ghe,dathanhtoan,khachhang,xuatchieu,IdDoiTuongMua) values("+dsVe[i].Ghe.ToString()+",0,"+dsVe[i].KhachHang.ToString()+","+dsVe[i].XuatChieu+","+dsVe[i].IdDoiTuongMua+")";
                command.CommandText = query;
                command.ExecuteNonQuery();
            }
        }
        public VeDTO[] DsDatTruoc(XuatChieuDTO xuatChieuDto)
        {
            connect();
            string query = "select * from ve where dathanhtoan=0 and xuatchieu='" + xuatChieuDto.IdXuatChieu + "'";
            adapter = new SqlDataAdapter(query, connection);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            DataTable dataTable = dataset.Tables[0];
            int n = dataTable.Rows.Count;
            VeDTO[] dsVe = new VeDTO[n];
            for (int i = 0; i < n; i++)
            {
                object ve = GetDataFromDataRow(dataTable, i);
                dsVe[i] = (VeDTO)ve;
            }
            return dsVe;
        }

        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
            VeDTO veDto = new VeDTO();
            veDto.IdVe = Convert.ToInt32(dt.Rows[i]["IdVe"].ToString());
            veDto.Ghe = Convert.ToInt32(dt.Rows[i]["Ghe"].ToString());
            veDto.KhachHang = Convert.ToInt32(dt.Rows[i]["KhachHang"].ToString());
            veDto.XuatChieu = Convert.ToInt32(dt.Rows[i]["XuatChieu"].ToString());
            veDto.DaThanhToan =false;
            return veDto;
        }

        public void HuyVe(int idVe)
        {
            connect();
            string query = "delete from ve where idve="+idVe;
            executeNonQuery(query);
        }

        public void HuyTatCaVeThuocXuatChieu(int idxuatchieu)
        {
            connect();
            string query = "delete from ve where ve.dathanhtoan=0 and xuatchieu="+idxuatchieu;
            executeNonQuery(query);
        }

        public void ThanhToanVe(int idve)
        {
            connect();
            string query = "update ve set dathanhtoan=1 where idve="+idve;
            executeNonQuery(query);
        }
    }
}
